package gui_layer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;

import model_layer.Sale;
import model_layer.StaffMember;
import ctr_layer.SaleCtr;
@SuppressWarnings("serial")
public class AccountDetailsGUI extends JInternalFrame
{
	private StaffMember staff_member;
	private SaleCtr sale_ctr;
	private JTextField fname_tf;
	private JTextField lname_tf;
	private JTextField phone_nr_tf;
	private JTextField email_tf;
	private JTextField type_tf;
	private JTextField username_tf;
	private JTable table;
	private JPanel panel_1;
	private JLabel total_sales_lbl;
	private String[] column_names = { "id", "Date", "Customer", "Total price" };
	private JPanel content_pane;

	/**
	 * Create the frame.
	 */
	public AccountDetailsGUI(StaffMember sm)
	{
		content_pane = new JPanel();
		setTitle("Account Details");
		try
		{
			setFrameIcon(new ImageIcon(AccountDetailsGUI.class.getResource("/img/icon.png")));
		}
		catch (Exception e)
		{	
		}
		content_pane.setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
		content_pane.setBorder(new EmptyBorder(5, 5, 5, 5));
		content_pane.setLayout(new BorderLayout(0, 0));
		setContentPane(content_pane);
		staff_member = sm;
		sale_ctr = new SaleCtr();
		setBounds(100, 100, 570, 300);
		setResizable(true);
		setIconifiable(true);
		setClosable(true);
		setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
		JPanel panel = new JPanel();
		panel.setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
		getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_3 = new JPanel();
		panel_3.setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
		panel.add(panel_3, BorderLayout.EAST);
		
		JLabel lblTotalSales = new JLabel("Total Sales:");
		lblTotalSales.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblTotalSales.setForeground(Color.WHITE);
		panel_3.add(lblTotalSales);
		
		total_sales_lbl = new JLabel("New label");
		total_sales_lbl.setFont(new Font("Helvetica Neue", Font.BOLD, 17));
		total_sales_lbl.setForeground(Color.WHITE);
		panel_3.add(total_sales_lbl);
		
		panel_1 = new JPanel();
		panel_1.setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
		panel_1.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(panel_1, BorderLayout.EAST);
		GridBagLayout gbl_panel_1 = new GridBagLayout();
		gbl_panel_1.columnWidths = new int[]{0, 0, 0};
		gbl_panel_1.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
		gbl_panel_1.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gbl_panel_1.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		panel_1.setLayout(gbl_panel_1);

		make_lbls();
		make_tfs();
		make_option_btns();
		make_table();
		fill_table();
		fill_fields();

	}
	
	private void make_lbls()
	{
		JLabel lblFirstName = new JLabel("First name");
		lblFirstName.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblFirstName.setForeground(Color.WHITE);
		GridBagConstraints gbc_lblFirstName = new GridBagConstraints();
		gbc_lblFirstName.anchor = GridBagConstraints.WEST;
		gbc_lblFirstName.insets = new Insets(0, 0, 5, 5);
		gbc_lblFirstName.gridx = 0;
		gbc_lblFirstName.gridy = 0;
		panel_1.add(lblFirstName, gbc_lblFirstName);
		
		JLabel lblLastName = new JLabel("Last Name");
		lblLastName.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblLastName.setForeground(Color.WHITE);
		GridBagConstraints gbc_lblLastName = new GridBagConstraints();
		gbc_lblLastName.anchor = GridBagConstraints.WEST;
		gbc_lblLastName.insets = new Insets(0, 0, 5, 5);
		gbc_lblLastName.gridx = 0;
		gbc_lblLastName.gridy = 1;
		panel_1.add(lblLastName, gbc_lblLastName);
		
		JLabel lblPhoneNumber = new JLabel("Phone number");
		lblPhoneNumber.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblPhoneNumber.setForeground(Color.WHITE);
		GridBagConstraints gbc_lblPhoneNumber = new GridBagConstraints();
		gbc_lblPhoneNumber.insets = new Insets(0, 0, 5, 5);
		gbc_lblPhoneNumber.anchor = GridBagConstraints.WEST;
		gbc_lblPhoneNumber.gridx = 0;
		gbc_lblPhoneNumber.gridy = 2;
		panel_1.add(lblPhoneNumber, gbc_lblPhoneNumber);
		
		JLabel lblEmail = new JLabel("Email");
		lblEmail.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblEmail.setForeground(Color.WHITE);
		GridBagConstraints gbc_lblEmail = new GridBagConstraints();
		gbc_lblEmail.anchor = GridBagConstraints.WEST;
		gbc_lblEmail.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmail.gridx = 0;
		gbc_lblEmail.gridy = 3;
		panel_1.add(lblEmail, gbc_lblEmail);
		
		JLabel lblType = new JLabel("Type");
		lblType.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblType.setForeground(Color.WHITE);
		GridBagConstraints gbc_lblType = new GridBagConstraints();
		gbc_lblType.insets = new Insets(0, 0, 5, 5);
		gbc_lblType.anchor = GridBagConstraints.WEST;
		gbc_lblType.gridx = 0;
		gbc_lblType.gridy = 4;
		panel_1.add(lblType, gbc_lblType);
		
		JLabel lblUsername = new JLabel("Username");
		lblUsername.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblUsername.setForeground(Color.WHITE);
		GridBagConstraints gbc_lblUsername = new GridBagConstraints();
		gbc_lblUsername.insets = new Insets(0, 0, 0, 5);
		gbc_lblUsername.anchor = GridBagConstraints.WEST;
		gbc_lblUsername.gridx = 0;
		gbc_lblUsername.gridy = 5;
		panel_1.add(lblUsername, gbc_lblUsername);
	}
	
	private void make_tfs()
	{
		fname_tf = new JTextField();
		fname_tf.setEditable(false);
		fname_tf.setEnabled(false);
		fname_tf.setBorder(new RoundedCornerBorder());
		fname_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		GridBagConstraints gbc_fname_tf = new GridBagConstraints();
		gbc_fname_tf.insets = new Insets(0, 0, 5, 0);
		gbc_fname_tf.fill = GridBagConstraints.HORIZONTAL;
		gbc_fname_tf.gridx = 1;
		gbc_fname_tf.gridy = 0;
		panel_1.add(fname_tf, gbc_fname_tf);
		fname_tf.setColumns(10);
		
		lname_tf = new JTextField();
		lname_tf.setEnabled(false);
		lname_tf.setBorder(new RoundedCornerBorder());
		lname_tf.setEditable(false);
		lname_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		GridBagConstraints gbc_lname_tf = new GridBagConstraints();
		gbc_lname_tf.insets = new Insets(0, 0, 5, 0);
		gbc_lname_tf.fill = GridBagConstraints.HORIZONTAL;
		gbc_lname_tf.gridx = 1;
		gbc_lname_tf.gridy = 1;
		panel_1.add(lname_tf, gbc_lname_tf);
		lname_tf.setColumns(10);
		
		phone_nr_tf = new JTextField();
		phone_nr_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		phone_nr_tf.setBorder(new RoundedCornerBorder());
		phone_nr_tf.setEditable(false);
		phone_nr_tf.setEnabled(false);
		GridBagConstraints gbc_phone_nr_tf = new GridBagConstraints();
		gbc_phone_nr_tf.insets = new Insets(0, 0, 5, 0);
		gbc_phone_nr_tf.fill = GridBagConstraints.HORIZONTAL;
		gbc_phone_nr_tf.gridx = 1;
		gbc_phone_nr_tf.gridy = 2;
		panel_1.add(phone_nr_tf, gbc_phone_nr_tf);
		phone_nr_tf.setColumns(10);
		
		email_tf = new JTextField();
		email_tf.setEditable(false);
		email_tf.setEnabled(false);
		email_tf.setBorder(new RoundedCornerBorder());
		email_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		GridBagConstraints gbc_email_tf = new GridBagConstraints();
		gbc_email_tf.insets = new Insets(0, 0, 5, 0);
		gbc_email_tf.fill = GridBagConstraints.HORIZONTAL;
		gbc_email_tf.gridx = 1;
		gbc_email_tf.gridy = 3;
		panel_1.add(email_tf, gbc_email_tf);
		email_tf.setColumns(10);
		
		type_tf = new JTextField();
		type_tf.setEditable(false);
		type_tf.setEnabled(false);
		type_tf.setBorder(new RoundedCornerBorder());
		type_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		GridBagConstraints gbc_type_tf = new GridBagConstraints();
		gbc_type_tf.insets = new Insets(0, 0, 5, 0);
		gbc_type_tf.fill = GridBagConstraints.HORIZONTAL;
		gbc_type_tf.gridx = 1;
		gbc_type_tf.gridy = 4;
		panel_1.add(type_tf, gbc_type_tf);
		type_tf.setColumns(10);
		
		username_tf = new JTextField();
		username_tf.setEditable(false);
		username_tf.setEnabled(false);
		username_tf.setBorder(new RoundedCornerBorder());
		username_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		GridBagConstraints gbc_username_tf = new GridBagConstraints();
		gbc_username_tf.fill = GridBagConstraints.HORIZONTAL;
		gbc_username_tf.gridx = 1;
		gbc_username_tf.gridy = 5;
		panel_1.add(username_tf, gbc_username_tf);
		username_tf.setColumns(10);
	}
	
	private void make_table()
	{
		table = new JTable();
		
		table.getTableHeader().setDefaultRenderer(new DefaultTableCellRenderer()
		{
			  {
				  float[] values = Color.RGBtoHSB(255, 104, 0, null);
				  setBackground(Color.getHSBColor(values[0], values[1], values[2]));
				  setOpaque(true);
				  setForeground(Color.WHITE);
				  this.setFont(new Font("Helvetica Neue", Font.BOLD, 12));
			  }
		});
		float[] values = Color.RGBtoHSB(255, 104, 0, null);
		table.setFont(new Font("Helvetica Neue", Font.PLAIN, 11));
		table.setFillsViewportHeight(true);
		table.setAutoCreateRowSorter(true);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.getTableHeader().setReorderingAllowed(false);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		table.setBorder(new EmptyBorder(3, 6, 3, 6));
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBorder(new RoundedCornerBorder());
		scrollPane.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		getContentPane().add(scrollPane, BorderLayout.CENTER);
		
		table.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{
				if (e.getClickCount() == 2)
				{
					details();
				}
			}
		});
		
		table.setModel(new MyTableModel(new Object[0][4], column_names));
		table.removeColumn(table.getColumnModel().getColumn(0));
	}
	
	private void make_option_btns()
	{
		float[] values = Color.RGBtoHSB(255, 104, 0, null);
		JPanel panel_2 = new JPanel();
		panel_2.setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
		getContentPane().add(panel_2, BorderLayout.SOUTH);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_4 = new JPanel();
		panel_4.setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
		panel_2.add(panel_4, BorderLayout.WEST);
		
		JButton btnDetails = new JButton("Details");
		btnDetails.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnDetails.setForeground(Color.WHITE);
		btnDetails.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnDetails.setContentAreaFilled(false);
		btnDetails.setOpaque(true);
		btnDetails.setBorder(new RoundedCornerBorder());
		btnDetails.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				details();
			}
		});
		panel_4.add(btnDetails);
		
		JButton btnChart = new JButton("Chart");
		btnChart.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnChart.setForeground(Color.WHITE);
		btnChart.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnChart.setContentAreaFilled(false);
		btnChart.setOpaque(true);
		btnChart.setBorder(new RoundedCornerBorder());
		btnChart.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				make_chart();
				
			}
		});
		panel_4.add(btnChart);
		
		JPanel panel_5 = new JPanel();
		panel_5.setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
		panel_2.add(panel_5, BorderLayout.EAST);
		
		JButton btnClose = new JButton("Close");
		btnClose.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnClose.setForeground(Color.WHITE);
		btnClose.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnClose.setContentAreaFilled(false);
		btnClose.setOpaque(true);
		btnClose.setBorder(new RoundedCornerBorder());
		btnClose.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				dispose();
			}
		});
		panel_5.add(btnClose);
	}
	
	private void fill_table()
	{
		List<Sale> sales = staff_member.getMade_sales();
		Object[][] filling = new Object[sales.size()][4];
		Object[] data;
		int i = 0;
		for (Sale sale : sales)
		{
			data = new Object[4];
			data[0] = sale.getId();
			data[1] = GUIUtilities.convert_date_to_string(sale.getSale_date());
			data[2] = sale.getCustomer().getName();
			data[3] = sale.getTotal_price();
			filling[i] = data;
			i++;
		}
		MyTableModel mtm = (MyTableModel) table.getModel();
		mtm.setDataVector(filling, column_names);
		total_sales_lbl.setText(String.valueOf(staff_member.get_total_price_of_sales()));
		table.removeColumn(table.getColumnModel().getColumn(0));
	}
	
	private void fill_fields()
	{
		fname_tf.setText(staff_member.getFname());
		lname_tf.setText(staff_member.getLname());
		phone_nr_tf.setText(staff_member.getPhone_nr());
		email_tf.setText(staff_member.getEmail());
		String[] types = { "Employee", "Manager", "Administrator" };
		type_tf.setText(types[staff_member.getPrivilage_level()]);
		username_tf.setText(staff_member.getLogin());
	}
	
	private void details()
	{
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			int id = (int) table.getModel().getValueAt(row, 0);
			NewSaleGUI ns_gui = new NewSaleGUI(sale_ctr.find_sale(id));
			this.getDesktopPane().add(ns_gui);
			Point window_corner = this.getLocation();
			ns_gui.setLocation((int) window_corner.getX() + 15, (int) window_corner.getY() + 15);
			ns_gui.setVisible(true);
		}
	}
	
	private void make_chart()
	{
		ChartCreator cc = new ChartCreator(staff_member);
		this.getDesktopPane().add(cc);
		Point current_loction = this.getLocation();
		cc.setLocation((int) current_loction.getX() +15, (int) current_loction.getY() + 15);
		cc.setVisible(true);
	}

}
